package display2012;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import interfaces.IDisplay;

public class DisplayTCPClient extends Thread implements IDisplay {

	protected IDisplay display;
	protected Socket clientSocket;
	protected boolean stop;
	
	public DisplayTCPClient(IDisplay display,String IP, int port) throws UnknownHostException, IOException {
		this.display = display;
		clientSocket = new Socket(IP, port);
		stop = false;
	}
	
	@Override
	public void update(String v) {
		display.update(v);
	}

	@Override
	public String getCurVarToShow() {
		return display.getCurVarToShow();
	}
	
	public void stopRunning() {
		stop = true;
	}
	
	@Override
	public void run() {
		InputStream in;
		int value;
		try {
			in = clientSocket.getInputStream();
			while(!stop){
				value = in.read();
				update(""+value);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
